package com.utonw.carloan.utils;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.utonw.carloan.R;

/**
 * Created by SEELE on 2017/3/24.
 */

public class ViewUtils {

    public static void initContentView(Activity activity, int preLayoutId, int replaceLayoutId) {
        FrameLayout view = (FrameLayout) activity.findViewById(preLayoutId);
        view.removeAllViewsInLayout();//移除当前LinearLayout的内容
        view.addView(addView(replaceLayoutId));//添加XML到LinearLayout
    }

    public static void initContentView(Activity activity, int targetId, int preLayoutId, int replaceLayoutId) {
        FrameLayout view = (FrameLayout) activity.findViewById(targetId).findViewById(preLayoutId);
        view.removeAllViewsInLayout();//移除当前LinearLayout的内容
        view.addView(addView(replaceLayoutId));//添加XML到LinearLayout
    }

    //追加替换EditText的hint字段
    public static void initContentView(Activity activity, int targetId, int preLayoutId, int replaceLayoutId, String editWords) {
        FrameLayout view = (FrameLayout) activity.findViewById(targetId).findViewById(preLayoutId);
        view.removeAllViewsInLayout();//移除当前LinearLayout的内容
        view.addView(addView(replaceLayoutId, editWords));//添加XML到LinearLayout
    }

    //增加联系人布局
    public static View addExtraContractView(int layoutId) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, UIUtils.dip2px(10), 0, 0);
        View view = UIUtils.inflate(layoutId);
        view.setLayoutParams(lp);
        return view;
    }

    //增加联系人布局
    public static View addItemView(int layoutId) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, 0, 0, 0);
        View view = UIUtils.inflate(layoutId);
        view.setLayoutParams(lp);
        return view;
    }


    //默认替换布局
    private static View addView(int layoutId) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        View view = UIUtils.inflate(layoutId);
        view.setLayoutParams(lp);
        return view;
    }


    //替换布局+可替换hint字段
    private static View addView(int layoutId, String editWords) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        View view = UIUtils.inflate(layoutId);
        switch (layoutId) {
            case R.layout.choosebar_item_right_edittext:
                EditText et = (EditText) view.findViewById(R.id.et_item_please_input_none);
                et.setHint(editWords);
                view.setLayoutParams(lp);
                break;

            case R.layout.choosebar_item_right_edittext_with_tips:
                TextView tv = (TextView) view.findViewById(R.id.tv_item_right_tips);
                tv.setText(editWords);
                view.setLayoutParams(lp);
                break;

        }

        return view;

    }
}
